import java.util.*;

public class IrSrArManagment {
    // ir1-sr1-ar1
    // ir1-sr1-ar2
    // ir2-sr2-ar3
    // ir2-sr2-ar2
    public static void main(String[] args) {
        List<Relation> relationList = Arrays.asList(new Relation("IR1", "SR1"), new Relation("IR2", "SR2"), new Relation("SR1", "AR1"), new Relation("SR1", "AR2"), new Relation("SR2", "AR3"), new Relation("SR2", "AR2"));
        List<GivedAr> givedArs = Arrays.asList(new GivedAr("AR1", 10, true), new GivedAr("AR2", 10, false), new GivedAr("AR3", 90, true));
        int result = handle(relationList, givedArs, 90);
        System.out.println(result);
    }

    private static final String IR = "IR";

    private static final String SR = "SR";

    private static Map<String, Set<String>> irMap = new HashMap<>();

    private static Map<String, String> srToIrMap = new HashMap<>();


    /**
     * 根据结果反向推导数据结构
     *
     * @param relationList
     * @param givedArs
     * @param threshold
     * @return
     */
    public static int handle(List<Relation> relationList, List<GivedAr> givedArs, int threshold) {
        // 初始化关系
        initMap(relationList);
        // 计算结果
        int result = 0;
        for (Set<String> ars : irMap.values()) {
            // 根据对象的某个属性求和
            int actual = givedArs.stream().filter(v -> ars.contains(v.arName)).filter(v -> v.isGived).mapToInt(v -> v.time).sum();
            int sum = givedArs.stream().filter(v -> ars.contains(v.arName)).mapToInt(v -> v.time).sum();
            double itemResult = (double) actual / sum * 100;
            if (itemResult >= threshold) {
                result++;
            }
        }
        return result;
    }

    private static void initMap(List<Relation> relationList) {
        for (Relation relation : relationList) {
            if (relation.parentName.startsWith(IR)) {
                srToIrMap.put(relation.childName, relation.parentName);
            }
            if (relation.parentName.startsWith(SR)) {
                String ir = srToIrMap.get(relation.parentName);
                Set<String> value;
                if (irMap.containsKey(ir)) {
                    value = irMap.get(ir);
                } else {
                    value = new HashSet<>();
                }
                value.add(relation.childName);
                irMap.put(ir, value);
            }
        }
    }

    public static class Relation {
        public Relation(String parentName, String childName) {
            this.parentName = parentName;
            this.childName = childName;
        }

        public String parentName;

        public String childName;
    }

    // 交付的ar
    public static class GivedAr {
        public String arName;

        public GivedAr(String arName, int time, boolean isGived) {
            this.arName = arName;
            this.time = time;
            this.isGived = isGived;
        }

        public int getTime() {
            return time;
        }

        public void setTime(int time) {
            this.time = time;
        }

        public int time;

        public boolean isGived;
    }
}
